package com.ch3cooh.registry.component;

import com.ch3cooh.registry.config.ZkProperties;
import com.ch3cooh.registry.pojo.RegisterInfo;
import com.ch3cooh.registry.service.RegisterService;
import com.ch3cooh.rpc.util.SpringUtils;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
@Log
public class DoRegister implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    RegisterService registerService;
    @Autowired
    ZkProperties zkProperties;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        RegisterInfo registerInfo = registerService.doRegister();
        if ( zkProperties.isAutoRegster() && registerInfo != null ){
            log.info(String.format("%s(%s) 已注册到zookeeper上",registerInfo.getServerName(),registerInfo.getHostUrl() ));
        }

        //测个试
       // UserService userService = (UserService)SpringUtils.getBean("userService");
       // System.err.println(userService);
    }

}
